package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.kaishengit.entity.Idea;
import com.kaishengit.entity.User;
import com.kaishengit.service.UserService;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.Page;
import com.kaishengit.util.db.RowMapper;

public class IdeaDao {
	
	private DBHelp<Idea> db = new DBHelp<Idea>();
	
	public void save(Idea idea){
		String sql="insert into t_idea (title,content,createtime,userId,projectId) values(?,?,?,?,?)";
		db.executeSQL(sql,idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getUserId(),idea.getProjectId());
	}
	
	
	public void update(Idea idea){
		String sql="update t_idea set title=?,content=?,createtime=?,userId=?,projectId=?where id=?";
		db.executeSQL(sql, idea.getTitle(),idea.getContent(),idea.getCreatetime(),idea.getUserId(),idea.getProjectId(),idea.getId());
	}
	
	public void delById(int id){
		String sql="delete from t_idea where id=?";
		db.executeSQL(sql,id);
	}
	

	
	private int countByProjectId(int pid) {
		String sql="select count(*) from t_idea where projectId=?";
		return db.executeQuery(sql, pid).intValue();
	}
	
	public Idea findByIdonly(int id) {
		String sql="select * from t_idea where id=?";
		return db.executeQueryToObject(sql, new IdeaRowMapper(),id);
	}
	
	public List<Idea> findByProjectId(Idea idea){
		String sql="select id,title,content,cratetime,userId from t_idea where projectId=?";
		return db.executeQueryToList(sql, new IdeaRowMapper(), "projectId");
	}
	
	
	public Idea findById(int id){
		String sql = "select * from t_idea where id = ?";
		return db.executeQueryToObject(sql, new RowMapper<Idea>() {

			public Idea mapRow(ResultSet resultSet) throws SQLException {
				Idea idea = new Idea();
				idea.setId(resultSet.getInt("id"));
				idea.setTitle(resultSet.getString("title"));
				idea.setContent(resultSet.getString("content"));
				idea.setCreatetime(resultSet.getString("createtime"));
				idea.setUserId(resultSet.getInt("userId"));
				idea.setProjectId(resultSet.getInt("projectId"));
				User user = new UserDao().findById(resultSet.getInt("userId"));
				idea.setUser(user);
				return idea;
			}
		}, id);
	}
	
	
	public Page<Idea> findCommentByProjectId(int nowPageNum, int id) {
		
		Page<Idea> page = new Page<Idea>(nowPageNum,countByProjectId(id));
		String sql = "select * from t_idea where projectId=? order by createtime desc limit ?,?";
		List<Idea> list = db.executeQueryToList(sql, new RowMapper<Idea>() {

			public Idea mapRow(ResultSet resultSet) throws SQLException {
				Idea idea = new Idea();
				idea.setId(resultSet.getInt("id"));
				idea.setTitle(resultSet.getString("title"));
				idea.setContent(resultSet.getString("content"));
				idea.setCreatetime(resultSet.getString("createtime"));
				idea.setUserId(resultSet.getInt("userId"));
				idea.setProjectId(resultSet.getInt("projectId"));
				User user = new UserDao().findById(resultSet.getInt("userId"));
				idea.setUser(user);
				return idea;
			}
		}, id,page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
	}
	
	
	
	
	private class  IdeaRowMapper implements RowMapper<Idea>{
		public Idea mapRow(ResultSet rs) throws SQLException {
			Idea idea = new Idea();
			
			idea.setId(rs.getInt("id"));
			idea.setTitle(rs.getString("title"));
			idea.setContent(rs.getString("content"));
			idea.setCreatetime(rs.getString("createtime"));
			idea.setUserId(rs.getInt("userId"));
			idea.setProjectId(rs.getInt("projectId"));
			idea.setUser(new UserService().findById(rs.getInt("userId")));
			
			return idea;
		}
		
	}
	
}
